import  _ from 'lodash'
export default class File {

    path = 'temp'
    name = 'untitled'
    content = undefined
    isPersisted = false
    lastPersisted = _.now()
    isDirty = true
    debugStatus = false
    runStatus = false
    hashCode = undefined
    view = undefined

  constructor(attrs){
    this.path = attrs.path
    this.name = attrs.name
    this.content = attrs.content
    this.isPersisted = attrs.isPersisted
    this.lastPersisted = _.now()
    this.isDirty = true
    this.debugStatus = false
    this.runStatus = false
    this.hashCode = undefined
    this.view = undefined

  }

  generateHash = function (hashCode) {
      var newHash = 0;
      var l = hashCode.length;
      var i = 0;
      if (l > 0) {
          while (i < l) {
              newHash = (newHash << 5) - newHash + hashCode.charCodeAt(i++) | 0;
          }
      }
      return newHash;
  };

  save = function(isTriggeredBySaveFlow){
      if(!_.isNil(this._storage.get(this.id))){
          if (isTriggeredBySaveFlow == true) {
              this.setHash(this.generateHash(this.getContent().trim()));
          }
          this._storage.update(this);
      } else {
          this._storage.create(this);
      }
      return this;
  };

  setHash = function (hashCode) {
    this.hashCode=hashCode
      return this;
  };

  setPath = function(path){
      this.set('path', path);
      return this;
  };

  setStorage = function(storage){
      this._storage = storage;
      return this;
  };

  setPersisted = function(isPersisted){
    this.isPersisted=isPersisted
      return this;
  };

  setLastPersisted = function(lsatPersisted){
      this.set('lastPersisted', lsatPersisted);
      return this;
  };

  setDirty = function(isDirty){
      this.set('isDirty', isDirty);
      this.trigger('dirty-state-change', isDirty);
      return this;
  };

  setName = function(name){
      this.set('name', name);
      return this;
  };

  setRunStatus = function(status){
      this.set('runStatus', status);
      return this;
  };

  setDebugStatus = function(status){
      this.set('debugStatus', status);
      return this;
  };

  setContent = function(name){
      this.set('content', name);
      return this;
  };

  setView = function(view){
      this.set('view', view);
      return this;
  };

  getView = function () {
      return this.get('view');
  };

  getHash = function () {
      return this.get('hashCode');
  };

  getPath = function(){
      return this.get('path')
  };

  getName = function(){
      return this.name
  };

  getRunStatus = function(){
      return this.get('runStatus')
  };

  getDebugStatus = function(){
      return this.get('debugStatus')
  };

  getContent = function(){
      return this.get('content')
  };

  getLastPersisted = function(){
      return this.get('lastPersisted');
  };


  isPersisted = function(){
      return this.isPersisted
  };

  isDirty = function(){
      return this.get('isDirty')
  };

  isStopProcessRunning = function () {
      return this.get('stopProcessRunning');
  };

  setStopProcessRunning = function(stopTriggered){
      this.set('stopProcessRunning', stopTriggered);
      return this;
  };


}
